package com.example.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.algorithm.CompareDate;
import com.example.config.MyService;
import com.example.entity.User;
import com.example.repository.UserRepository;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Date;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;
    @Autowired
    private MyService myService;

    @Override
    public User getUser(String userId) {
        User user;
        try {
            user = userRepository.findById(userId).get();
        }catch (Exception e){
            return null;
        }
        Date date = new Date();
        if(CompareDate.isSameDay(date, user.getLastTime())){
            user.setSignIn(true);
        }else{
            user.setSignIn(false);
        }
        return user;
    }

    @Override
    public void signIn(String userId) {
        User user = userRepository.findById(userId).get();
        Date date = new Date();
        if(!CompareDate.isSameDay(date, user.getLastTime())){
            //当天没有签到，修改数据库，实现当天签到
            user.setDays(user.getDays() + 1);
            user.setLastTime(date);
            user.setUpdateAt(date);
            user.setUpdateBy("admin");
            userRepository.save(user);
        }
    }

    @Override
    public String getUserId(String code) {
        String url = myService.getWeChatApi() + "?appid=" + myService.getWeChatAppId() + "&secret=" + myService.getWeChatSecret() +
                "&js_code="+code+"&grant_type=" + myService.getWeChatGrantType();
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        HttpEntity<String> entity = new HttpEntity<String>(headers);
        String stringbody = restTemplate.exchange(url, HttpMethod.GET,entity,String.class).getBody();
        JSONObject json = JSON.parseObject(stringbody);
        String userId = json.getString("openid");
        User user = getUser(userId);
        if(user == null){
            user = new User();
            user.setId(userId);
            user.setCreateBy("admin");
            user.setCreateAt(new Date());
            user.setDel(false);
            user.setUpdateAt(new Date());
            user.setUpdateBy("admin");
            userRepository.save(user);
        }
        return user.getId();
    }

    @Override
    public void saveUser(User user) {
        userRepository.save(user);
    }

    @Override
    public void addRecord(String userId) {
        User user = getUser(userId);
        user.setSum(user.getSum()+1);
        user.setUpdateAt(new Date());
        user.setUpdateBy("admin");
        saveUser(user);
    }

    @Override
    public void addLevel(String userId) {
        User user = getUser(userId);
        user.setLevel(user.getLevel()+1);
        user.setUpdateAt(new Date());
        user.setUpdateBy("admin");
        saveUser(user);
    }

}

